<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Default
 *
 * @author Acer
 */
class Content_Controller_Admin_Menu extends MVC_AbstractAdminController {
    
    private $menuModel;
    
    public function __construct() {
        
        parent::__construct();
        
        $this->menuModel = new Content_Model_Admin_Menu();
    }
    
    public function index() {
        
        $enums = $this->menuModel->db->enums('content_menu', 'menu_position');
    
        $menuArray = $this->menuModel->parentChildContentMenu();
        
        $table  = $this->menuModel->pagination->render();
        
        $table .= '<table class="table table-striped table-bordered">';
        
        $table .=   '<thead>';
        
        $table .=       '<tr>';
        
        $table .=           '<form action ="'.$this->getLink('Content', 'Menu', 'index').'" 
                                   method = "GET"
                                   id     = "indexFilterForm"
                                   name   = "indexFilterForm"
                             >';
        
        $table .=           Form::form_hidden(Config::get('app.module_uri_segment'), 'Content');                      
        
        $table .=           Form::form_hidden(Config::get('app.controller_uri_segment'), 'Menu');
        
        $table .=           Form::form_hidden(Config::get('app.action_uri_segment'), 'index');
        
        $table .=           '<th><input name="filter[id]" id="filter[id]" style="width:22px" value="'.fRequest::get('filter[id]').'"/></th>';
        
        $table .=           '<th><input name="filter[menu_title]" id="filter[menu_title]" value="'.fRequest::get('filter[menu_title]').'" style="width:250px" /></th>';
        
        $table .=           '<th>';
        
        $table .=               Form::form_dropdown('filter[menu_position]', 
                                                    $this->menuModel->db->enums('content_menu', 'menu_position'), 
                                                    fRequest::get('filter[menu_position]'),
                                                    'style="width:50px" class="toolbarSelect"');
        
        $table .=           '</th>';
        
        $table .=           '<th><input name="filter[permalink]" id="filter[permalink]" value="'.fRequest::get('filter[permalink]').'" style="width:250px" /></th>';
        
        $table .=           '<th><input name="filter[sorted]" id="filter[sorted]" value="'.fRequest::get('filter[sorted]').'" style="width:45px" /></th>';
        
        $table .=           '<th>';
        
        $table .=               Form::form_dropdown('filter[home_page]', 
                                                    $this->menuModel->db->enums('content_menu', 'home_page'), 
                                                    fRequest::get('filter[home_page]'),
                                                    'style="width:50px" class="toolbarSelect"');
        
        $table .=           '</th>';
        
        $table .=           '<th>';
        
        $table .=               Form::form_dropdown('filter[posted]', 
                                                    $this->menuModel->db->enums('content_menu', 'posted'), 
                                                    fRequest::get('filter[posted]'),
                                                    'style="width:50px" class="toolbarSelect"');
        
        $table .=           '</th>';
        
        $table .=           '<th class="center " style="text-align:right;">';
        
        $table .=               '<a id="indexFilterSubmit" class="btn btn btn-success" href="#">'.UtilTemplate::adminIcon(array('ok', 'white')).' Filter</a>
                             
                                 <a class="btn btn-danger" href="'.$this->getLink('Content', 'Menu', 'index').'">'.UtilTemplate::adminIcon(array('ok', 'white')).' Reset</a>';
        
        $table .=           '</th>';
        
        $table .=       '</form>';
        
        $table .=       '</tr>';
        
        $table .=       '<tr>';
        
        $table .=           '<th>ID</th>';
        
        $table .=           '<th>Title</th>';
        
        $table .=           '<th>Position</th>';
        
        $table .=           '<th>Permalink</th>';
        
        $table .=           '<th>Sorted</th>';
        
        $table .=           '<th>Home Page</th>';
        
        $table .=           '<th>Posted</th>';
        
        $table .=           '<th>Actions</th>';
 
        $table .=       '</tr>';
        
        $table .=   '</thead>';
        
        $table .=   '<tbody>';
        
        foreach($menuArray as $menu)
        {
            $table .= $this->tableMenuRow($menu);
            
            if(!empty($menu['children']))
            {
                foreach($menu['children'] as $child)
                {
                    $table .= $this->tableMenuRow($child);
                }
            }
        }
        
        $table .=   '</tbody>';

        $table .= '</table>';

        $js = <<<SCRIPT
            
            <script>
                
                $(document).ready(function(){
                
                    $('#indexFilterSubmit').click(function(){
                        
                        $('#indexFilterForm').submit();

                    });
                    
                });
            
            </script>
SCRIPT;
        
        return UtilTemplate::adminDataList($table, 'Menus', $toolbarArray = array()).$js;

    }
    
    private function tableMenuRow($menu)
    {
        $rowClass = $menu['parent_id'] == 0 ? 'parent' : 'child';
        
        $table  = '<tr>';
            
        $table .=   '<td>'.$menu['id'].'</td>';
         
        $table .=   '<td class="'.$rowClass.'">'.$menu['menu_title'].'</td>';

        $table .=   '<td>'.$menu['menu_position'].'</td>';

        $table .=   '<td>'.$menu['permalink'].'</td>';

        $table .=   '<td>'.$menu['sorted'].'</td>';

        $table .=   '<td>'.$menu['home_page'].'</td>';

        $table .=   '<td>'.$menu['posted'].'</td>';
       
        if($menu['parent_id'] == 0)
        {
            $addChildBtn = '<a class="btn btn-success" 
                     
                                href="'.$this->getLink('Content', 'Menu', 'add', array('id' => $menu['id'])).'">'

                                .UtilTemplate::adminIcon(array('plus', 'white')).

                                'Add Child

                            </a>';
        }
        else
        {
            $addChildBtn = '';
        }
        
        $table .= '<td class="center " style="text-align:right;">';
        
        $table .=   '<a class="btn btn-info" 
                     
                        href="'.$this->getLink('Content', 'Menu', 'edit', array('id' => $menu['id'])).'">'
                           
                        .UtilTemplate::adminIcon(array('edit', 'white')).
                           
                        'Edit
                        
                     </a>
                   '.$addChildBtn.'
                   <a class="btn btn-danger" 
                     
                      href="'.$this->getLink('Content', 'Menu', 'delete', array('id' => $menu['id'])).'">'
                           
                      .UtilTemplate::adminIcon(array('trash', 'white')).
                           
                      'Delete
                        
                   </a>
                        
                  </td>';
        
        $table .= '</tr>';
        
        return $table;
    }
    
    public function add() {
        
    }

    public function update() {
        
    }

    public function view() {
        
    }

    public function delete() {
        
    }

}